#pragma once

// Qt
#include <QtCore/QObject>
#include <QtCore/QList>

class Item : public QObject
{
	Q_OBJECT

public:
	typedef QList<Item *> TSubItems;

	/// Enum for the events that may happen with the child items.
	enum ChildAction
	{
		Add = 0, /// Someone tries to add new child item.
		Remove, /// Someone tries to remove one of child items.
		Build, /// Some of the child items must be built.
		Edit /// Need to show editor page for some child item.
	};

	/// Enum for the item states while building process is in progress.
	enum State
	{
		Building = 0,
		Complete,
		Error
	};

	/// ID is a unique string that is used to register the item in the build core.
	Item(const QString & aID);

	/// Return unique id of this item.
	virtual const QString & getID() const;

	/// Build current item.
	virtual bool build() = 0;

	/// Get child items of the current item.
	virtual const TSubItems & Item::getSubItems() const;

	/// This handler is called if some action happens with the child aItem. Handler returns true if it takes care
	/// of the all special actions nedded.
	/// Default handler provides:
	///  - accepting/deleting any of the subitems
	///  - editing properties
	///  - building any of the subitems.
	virtual bool childAction(Item::ChildAction aAction, Item * aItem);

private:
	/// Add new subitem.
	void addSubItem(Item * aItem);
	
	/// Remove child aItem.
	void removeSubItem(Item * aItem);

protected slots:
	/// This slot triggers when one of the subitems is changes its state.
	virtual void onSubItemStateChanged(Item::State aState);

signals:
	/// If the state of the item is changed this signal must be triggered to notify build core.
	void stateChanged(Item::State aState);

protected:
	QString mID;
	TSubItems mSubItems;
};